package com.example.controller;

import com.example.constants.MyConstants;
import com.example.pojo.User;
import com.example.resultType.Result;
import com.example.service.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


//用户登录controller
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserServiceImpl userService;


    /**
     * 登陆验证
     * @param username 用户名
     * @param password 密码
     * @return java.lang.String
     * @author zs
     * @creed: Talk is cheap,show me the code
     * @date 2021/8/25 11:55
     */
    @RequestMapping("/login/{username}/{password}")
    public String login(@PathVariable String username,
                        @PathVariable String password){
        //根据用户名获取登录用户
        User user = userService.getUserByUsername(username);

        Result<User> result = new Result<>();

        //登陆验证
        if(user.getPassword().equals(password)){
            result.setCode(MyConstants.SUCCESS);
            result.setData(user);
            result.setMessage("登陆成功");
        }else {
            result.setCode(MyConstants.FAILED);
            result.setMessage("用户名或密码错误");
        }
        return result.toString();
    }

    /**
     *
     * @param userId 根据id查询用户

     * @return java.lang.String
     * @author zs
     * @creed: Talk is cheap,show me the code
     * @date 2021/8/25 11:56
     */

    @RequestMapping("/getById/{userId}")
    public String getById(@PathVariable int userId){
        //根据用户名获取登录用户
        User user = userService.getUserById(userId);

        Result<User> result = new Result<>();

        //登陆验证
        if(user != null){
            result.setCode(MyConstants.SUCCESS);
            result.setData(user);
            result.setMessage("查询成功");
        }else {
            result.setCode(MyConstants.FAILED);
            result.setMessage("查询失败");
        }
        return result.toString();

    }


}
